package com.letv.so.servlet;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang.StringUtils;
import org.apache.log4j.Logger;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.serializer.SerializerFeature;
import com.letv.so.bean.AlbumBean;
import com.letv.so.bean.ExtranetAlbumBean;
import com.letv.so.constants.Constants;
import com.letv.so.util.SoKeyUtil;
import com.letv.so.util.StringUtil;
import com.letv.so.util.ThreadLocalUtil;

/**
 * cbase专辑所有站点接口
 * 
 * @author wenjian
 * 
 */
public class QueryCbaseAllWebSiteServlet extends AbstractQueryDataServlet {

	private static final long serialVersionUID = -2626713538347997214L;
	private Logger logger = Logger.getLogger(QueryCbaseAllWebSiteServlet.class);

	/**
	 * 专辑所有站点信息
	 */
	protected String query(Map<String, Object> rule) {
		long starttime = System.currentTimeMillis();
		String reData = null;
		String sid = (String)rule.get("sid");
		String pcp = (String) rule.get("pcp");
		String website_id_list = (String) rule.get("website_id_list");
		boolean ifCibn = false;    // 是否上cibn
        if(!StringUtils.isBlank(pcp) && Constants.PCP_FOR_CIBN.equals(pcp)) {
            ifCibn = true;
        }
        boolean ifTarget = !StringUtils.isBlank(website_id_list);
		try {
			List<ExtranetAlbumBean> eAlbumList = new ArrayList<ExtranetAlbumBean>();
			String soKey = (String) rule.get("soKey");
			String src = SoKeyUtil.getSrc(soKey);// 获得前缀vrs_,leso_
			String aKey = null;
			int idx = soKey.lastIndexOf("_")+1;
			// 获取专辑信息
			if (Constants.SRC_VRS.equals(src)) {
				aKey = "newmms_vrs_a_" + soKey.substring(idx);
			} else {
				aKey = "newmms_leso_a_" + soKey.substring(idx);
			}
			logger.info(sid+","+soKey+",query cbase val"+",rewrite soKey="+aKey);
			String soData = queryDataService.get(sid,"inc" + aKey);// 获得cbase数据
			if (StringUtils.isBlank(soData)) {
				soData = queryDataService.get(sid,aKey);// 获得cbase数据
			}
			if (StringUtils.isBlank(soData)) {
	            return "{}";
            }
			AlbumBean album = JSON.parseObject(soData, AlbumBean.class);
			
			String lesoAid = getLesoAid(soKey.substring(idx), src);// 解析sokey获得leso专辑ID
			if (lesoAid==null) {//乐视独家
				ExtranetAlbumBean eAlbum = new ExtranetAlbumBean();
				eAlbum.setVrsAid(album.getAid());
				eAlbum.setSite(Constants.WEBSITE_LETV);
				eAlbum.setDataType(Constants.ALBUM);
				eAlbum.setIsTv("1");
				eAlbum.setIsMobile("1");
				eAlbum.setIsPc("1");
				
				eAlbumList.add(eAlbum);
				album.setSiteList(eAlbumList);//添加site
            }else {
                List<String> soKeyList = CommonServlet.getWebsiteSokeyList(lesoAid, website_id_list);
    			List<String> soDataList =  queryDataService.getMulti(sid,soKeyList);
    			for (String data : soDataList) {
    				if (!StringUtil.isBlank(data)) {
    					ExtranetAlbumBean eAlbum = JSON.parseObject(data, ExtranetAlbumBean.class);
    					if (eAlbum != null) {
    					    if(!CommonServlet.websiteIfOn(ifTarget, eAlbum, ifCibn)) {
    					        continue;
    					    }
    						eAlbum.setVideoPlayUrls(null);// 此字段在此接口无用
    						eAlbum.setVideoList(null);
    						eAlbum.setIntro(null);
    						eAlbum.setNowEpisodes(null);
    						eAlbum.setEpisodes(null);
    						eAlbum.setOrderNum(null);
    						eAlbum.setTophot(null);
    						eAlbum.setCategory(null);
    						eAlbum.setPlayStreams(null);
    						eAlbum.setVideoDeadlinkUrls(null);
    						eAlbum.setIs_cibn_checked(null);
    						eAlbum.setPushflag(null);
    						
    						eAlbumList.add(eAlbum);
    					}
    				}
    			}
    			CommonServlet.websiteSort(ifTarget, eAlbumList);
    			album.setSiteList(eAlbumList);//添加site
			}
			//屏蔽外网不显示的字段
			album.setGlobalVids(null);
			album.setShortDesc(null);
			album.setVideoPlayUrls(null);
			album.setStarsPoster(null);
			album.setDirectoriesPoster(null);
			album.setActor(null);
			album.setActorName(null);
			album.setActorPlay(null);
			album.setAuthors(null);
			album.setCompose(null);
			album.setContentRating(null);
			album.setCopyright(null);
			album.setCountry(null);
			album.setDayCount(null);
			album.setMonthCount(null);
			album.setDirectory(null);
			album.setDeadlinks(null);
			album.setWeekCount(null);
			album.setTag(null);
			album.setDetailUrl(null);
			album.setDuration(null);
			album.setEnglishName(null);
			album.setIntro(null);
			album.setLanguage(null);
			album.setNameJianpin(null);
			album.setNameQuanpin(null);
			album.setPushFlag(null);
			album.setUrl(null);
			album.setViewPics(null);
			//屏蔽内网不显示的字段
			album.setFitAge(null);
			album.setIsdelete(null);
			album.setCtime(null);
			album.setMtime(null);
			album.setPayPlatform(null);
			album.setPayProduct(null);
			album.setPublishCompany(null);
			album.setRcompany(null);
			album.setRecordCompany(null);
			album.setTvid(null);
			album.setTvTitle(null);
			album.setWebsiteTitle(null);
			album.setVarietyShow(null);
			album.setDoubanid(null);
			album.setQname(null);
			album.setLetvReleaseDate(null);
			album.setOtherName(null);
			album.setDownloadPlatform(null);
			album.setDuration(null);
			album.setEnglishName(null);
			album.setHasHeight(null);
			album.setIsHomemade(null);
			album.setMaker(null);
			album.setSourceid(null);
			album.setSubname(null);
			album.setVidEpisode(null);
			album.setVideoType(null);
			album.setVideoTypeName(null);
			album.setVids(null);
			album.setStyle(null);
			album.setName_pinyin_abb(null);
			album.setPicOriginal(null);
			album.setPicOriginal(null);
			album.setPlayStreams(null);
			album.setPushFlag(null);
			album.setRelationid(null);
			album.setScreenwriter(null);
			album.setMobileTitle(null);
			album.setPlayCount(null);
			album.setPlayStatus(null);
			album.setStatus(null);
			
			reData = JSON.toJSONString(album, SerializerFeature.WriteTabAsSpecial);// 转为json格式
		} catch (Exception e) {
			logger.error(sid+",QueryCbaseServlet.query error", e);
			return "{}";
		}
        if (TRADITIONAL.contains(ThreadLocalUtil.getLang())){
            reData = converter.convert(reData);
        }
		logger.info(sid+",QueryCbaseAllWebSiteServlet, total time="+(System.currentTimeMillis()-starttime));
		return reData;
	}

	/**
	 * 解析sokey获得leso专辑ID
	 * 
	 * @param soKey
	 * @return
	 */
	private String getLesoAid(String aid, String src) {
		String lesoAid = "";
		if (Constants.SRC_VRS.equals(src)) { // sokey如果对应的是vrs的专辑需要一次查询，才能获得lesoAid
			String soKey = "newleso_la_" + aid;
			String soData = queryDataService.get(soKey);
			if (StringUtil.isBlank(soData)) {
				return null;
			}
			ExtranetAlbumBean eAlbum = JSON.parseObject(soData, ExtranetAlbumBean.class);
			lesoAid = eAlbum.getAid();

		} else if (Constants.SRC_LESO.equals(src)) { // sokey如果对应的是leso专辑，直接解析，即可获得lesoAid
			lesoAid = aid;
		}
		return lesoAid;
	}	
}
